package fr.cephb.joperon.webapp.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import fr.cephb.svg.SVG;
import fr.cephb.util.ResourceUtils;
import fr.cephb.xlink.XLink;

public class SVGTitle  extends BodyTagSupport
	{
	private static final long serialVersionUID = 1L;
	private static final String DEFAULT_STYLE="font-size:40px;font-style:normal;" +
			"font-variant:normal;font-weight:bold;font-stretch:normal;" +
			"fill-opacity:1;stroke:none;" +
			"stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;" +
			"stroke-opacity:1;font-family:Norasi;" +
			"text-anchor:start;text-align:start;" +
			"writing-mode:lr;line-height:125%;fill-rule:nonzero;";
	private static final int DEFAULT_WIDTH=640;
	private static final int DEFAULT_HEIGHT=400;
	/** svg Width */
	private int width=DEFAULT_WIDTH;
	/** svg Height */
	private int height=DEFAULT_HEIGHT;
	/** svg template */
	private String template;
	/** string */
	private String style=DEFAULT_STYLE;
	/***
	 * SVGTitle
	 */
	public SVGTitle()
		{
		try	{
			this.template=ResourceUtils.getContent(SVGTitle.class,"svg-title.svg");
			this.template = this.template.replaceAll("__SVG_NS__", SVG.NS)
							.replaceAll("__XLINK_NS__", XLink.NS);
			} 
		catch(Throwable err)
			{
			this.template=err.getMessage();
			}
		}
	
	@Override
	public int doStartTag() throws JspException
		{
		return EVAL_BODY_BUFFERED;
		}
	
	@Override
	public int doEndTag() throws JspException {
		try {
			String content= null;
			if(this.bodyContent!=null)
				{
				content= this.bodyContent.getString();
				}
			
			if(content!=null && this.width>0 && this.height>0)
				{
				super.pageContext.getOut().print(
				this.template
					.replaceAll("__WIDTH__", String.valueOf(this.width))
					.replaceAll("__HEIGHT__",String.valueOf(this.height))
					.replaceAll("__WIDTH2__", String.valueOf(this.width/2))
					.replaceAll("__HEIGHT2__",String.valueOf(this.height/2))
					.replaceAll("__CONTENT__", content)
					.replaceAll("__STYLE__", this.style)
				);
				
				}
			} 
		catch (IOException err)
			{
			throw new JspException(err);
			}
		return EVAL_PAGE;
		}

	@Override
	public void release() {
		this.width=DEFAULT_WIDTH;
		this.height=DEFAULT_HEIGHT;
		this.style=DEFAULT_STYLE;
		}
	
	public void setWidth(int width) {
		this.width = width;
	}
	
	public void setHeight(int height) {
		this.height = height;
	}
	
	public void setStyle(String style) {
		this.style = style;
		}
	
	}
